#include "ZigbeeControlProtocol.h"

ZigbeeControlProtocol::ZigbeeControlProtocol(QObject *parent) :
    BaseProtocol(parent)
{
    this->m_protocolType = ProtocolType::ZigbeeControlProtocol;
}

QList<Package> ZigbeeControlProtocol::parseData(QByteArray data)
{
    quint8  checkSteps = CHECK_HEAD;          //当前解析状态
    quint16 lengthCount;                 //数据长度计算                 //数据头索引计算
    quint16 bodyLength;
    QByteArray dataArray;
    QList<Package> packageList;

    for(int i=0; i<data.size(); i++)
    {
        quint8 byte = data.at(i);
        switch(checkSteps)
        {
            case CHECK_HEAD:   //检查数据头

                 if(byte == 0x7C)
                 {
                    lengthCount = 0;
                    bodyLength = 0;
                    dataArray.clear();
                    dataArray.append(0x7C);
                    checkSteps = CHECK_LENGTH;
                 }
            break;

            case CHECK_LENGTH:
                 dataArray.append(byte);
                 bodyLength = byte;
                 if(bodyLength > 50)
                     checkSteps = CHECK_HEAD;
                 else
                    checkSteps = ChECK_RESERVED;
                 break;

            case ChECK_RESERVED:
                 dataArray.append(byte);
                 checkSteps = CHECK_FUNCTION;
            break;

            case CHECK_FUNCTION:
                 dataArray.append(byte);
                 checkSteps = CHECK_BODY;
            break;

            case CHECK_BODY:    //接收数据
                 lengthCount++;
                 dataArray.append(byte);
                 if(lengthCount >= bodyLength)
                 {
                     lengthCount = 0;
                     checkSteps = CHECK_CHECKCODE;
                 }
                 break;

            case CHECK_CHECKCODE:
                 dataArray.append(byte);
                 if(CheckCode::computeSum(dataArray,0,dataArray.size()-1) == byte)
                 {
                     Package package;
                     package.setHead(dataArray.mid(0,1));
                     package.setLength(dataArray.mid(1,1).toHex().toInt(0,16));
                     package.setFunction(dataArray.mid(3,1).toHex().toInt(0,16));
                     package.setBody(dataArray.mid(4,bodyLength));
                     package.setCheckCode(byte);
                     packageList.append(package);
                 }
                 checkSteps = CHECK_HEAD;
        }
    }

    return packageList;
}

QByteArray ZigbeeControlProtocol::generateData(Package package)
{
    QByteArray data;
    quint8 length = package.body().size();

    data.append(0x7C);
    data.append(length);
    data.append(0x01);
    data.append(DataTypeConver::intToByteArray(package.function(),1));
    data.append(package.body());
    quint8 checkcode = CheckCode::computeSum(data,0,length+4);
    data.append(checkcode);

    return data;
}
